<?php
// jimport('infinity.csv');
abstract class CsvHelper {
	public static function csvFromFile($postname = 'file1', $files)
	{
		//setlocale(LC_ALL,array('zh_CN.gbk','zh_CN.gb2312','zh_CN.gb18030'));
		ini_set('auto_detect_line_endings',TRUE);
	    if($files[$postname]['error'] == UPLOAD_ERR_NO_FILE) {
			return null;
	    }
		if (($handle = fopen($files[$postname]['tmp_name'], "r")) !== FALSE) {
			# title
			$title = array();
			if (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
				for($i=0; $i<count($row); $i++) {
					$title []= $row[$i];
				}
			}
			
			# data body
			$results = array();
	    	while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
	    		$temp = array();
	    		for($i=0; $i<count($title); $i++) {
	    			$temp[$title[$i]] = $row[$i];
	    		}
	    		$results []= $temp;
	    	}
	    	
	    	return $results;
	    }
	}
}